/*
 * Copyright 2010. 
 * 
 * This document may not be reproduced, distributed or used 
 * in any manner whatsoever without the expressed written 
 * permission of xrkol Corp. 
 * 
 * $Rev: Rev $
 * $Author: Author $
 * $LastChangedDate: LastChangedDate $
 *
 */

package com.xrkol.sharelib.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xrkol.sharelib.dao.GuardianDao;
import com.xrkol.sharelib.entity.User;
import com.xrkol.sharelib.entity.Guardian;
import com.xrkol.sharelib.entity.Student;
import com.xrkol.sharelib.service.GuardianService;
import com.xrkol.sharelib.util.CollectionUtils;
import com.google.common.collect.Lists;

/**
 * @author peng.xia
 *
 */
@Service
@Transactional
public class GuardianServiceImpl implements GuardianService{
    
    @Autowired
    private GuardianDao guardianshipDao;

    @Override
    public void save(Guardian entity) {
        this.guardianshipDao.save(entity);
    }

    @Override
    public void update(Guardian entity) {
        this.guardianshipDao.update(entity);
    }

    @Override
    public void delete(String id) {
        this.guardianshipDao.delete(id);
    }

    @Override
    public Guardian findById(String id) {
        return this.guardianshipDao.findById(id);
    }

    @Override
    public List<Guardian> findByStudent(Student student) {
        return this.guardianshipDao.findByStudent(student);
    }

    @Override
    public Guardian findByStudentAndGuardian(User guardian, Student student) {
        return this.guardianshipDao.findByStudentAndGuardian(guardian, student);
    }

    @Override
    public List<Guardian> findByAdult(User adult) {
        return this.guardianshipDao.findByAdult(adult);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Guardian> getGuardiansByStudents(List<Student> students) {
        List<Guardian> guardians = Lists.newArrayList();
        for (Student student : students) {
            List<Guardian> part=this.guardianshipDao.findByStudent(student);
            guardians.addAll(part);
        }
        return CollectionUtils.removeDuplicate(guardians);
    }

}
